const wx2my = require('../../wx2my');

var http = require('../../utils/http.js');

var config = require('../../utils/config.js');

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    current: 1,
    pages: 0,
    sts: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.sts) {
      this.setData({
        sts: options.sts
      });
      this.loadOrderData(options.sts, 1);
    } else {
      this.loadOrderData(0, 1);
    }
  },

  /**
   * 加载订单数据
   */
  loadOrderData: function (sts, current) {
    var ths = this;
    wx2my.showLoading(); //加载订单列表

    var params = {
      url: "/p/myOrder/myOrder",
      method: "GET",
      data: {
        current: current,
        size: 10,
        status: sts
      },
      callBack: function (res) {
        //console.log(res);
        var list = [];

        if (res.current == 1) {
          list = res.records;
        } else {
          list = ths.data.list;
          Array.prototype.push.apply(list, res.records);
        }

        ths.setData({
          list: list,
          pages: res.pages,
          current: res.current
        });
        wx2my.hideLoading();
      }
    };
    http.request(params);
  },

  /**
   * 状态点击事件
   */
  onStsTap: function (e) {
    var sts = e.currentTarget.dataset.sts;
    this.setData({
      sts: sts
    });
    this.loadOrderData(sts, 1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current < this.data.pages) {
      this.loadOrderData(this.data.sts, this.data.current + 1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 查看物流
   */
  toDeliveryPage: function (e) {
    wx2my.navigateTo({
      url: '/pages/express-delivery/express-delivery?orderNum=' + e.currentTarget.dataset.ordernum
    });
  },

  /**
   * 取消订单
   */
  onCancelOrder: function (e) {
    var ordernum = e.currentTarget.dataset.ordernum;
    var ths = this;
    wx2my.showModal({
      title: '',
      content: '要取消此订单？',
      confirmColor: "#3e62ad",
      cancelColor: "#3e62ad",
      cancelText: '否',
      confirmText: '是',

      success(res) {
        if (res.confirm) {
          wx2my.showLoading({
            mask: true
          });
          var params = {
            url: "/p/myOrder/cancel/" + ordernum,
            method: "PUT",
            data: {},
            callBack: function (res) {
              //console.log(res);
              ths.loadOrderData(ths.data.sts, 1);
              wx2my.hideLoading();
            }
          };
          http.request(params);
        } else if (res.cancel) {//console.log('用户点击取消')
        }
      }

    });
  },

  /**
   * 付款
   */
  onPayAgain: function (e) {
    let orderNumbers = e.currentTarget.dataset.ordernum;
    let actualTotal = e.currentTarget.dataset.actualtotal;
    let data = {
      token: wx2my.getStorageSync('token'),
      order_num: e.currentTarget.dataset.ordernum,
      money: e.currentTarget.dataset.actualtotal,
      type: 1
    };
    app.wxRequest('mall_pay', data, function (res) {
      console.log(res);

      if (res.data.code == 1) {
        let data = res.data.data.payparams.success;
        wx.requestPayment({
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': function (res) {
            console.log(res);
            wx2my.navigateTo({
              url: '/pages/pay-result/pay-result?sts=1&orderNumbers=' + orderNumbers + "&actualTotal=" + actualTotal
            });
          },
          'fail': function (err) {
            console.log(err);
          }
        });
      } else if (res.data.code == 40002) {
        // 特殊用户下单
        wx2my.navigateTo({
          url: '/pages/pay-result/pay-result?sts=1&orderNumbers=' + orderNumbers + "&actualTotal=" + actualTotal
        });
      } else {
        app.toast(res.data.msg);
      }
    });
  },

  /**
   * 查看订单详情
   */
  toOrderDetailPage: function (e) {
    wx2my.navigateTo({
      url: '/pages/order-detail/order-detail?orderNum=' + e.currentTarget.dataset.ordernum
    });
  },

  /**
   * 确认收货
   */
  onConfirmReceive: function (e) {
    var ths = this;
    wx2my.showModal({
      title: '',
      content: '我已收到货？',
      confirmColor: "#eb2444",

      success(res) {
        if (res.confirm) {
          wx2my.showLoading({
            mask: true
          });
          var params = {
            url: "/p/myOrder/receipt/" + e.currentTarget.dataset.ordernum,
            method: "PUT",
            data: {},
            callBack: function (res) {
              //console.log(res);
              ths.loadOrderData(ths.data.sts, 1);
              wx2my.hideLoading();
            }
          };
          http.request(params);
        } else if (res.cancel) {//console.log('用户点击取消')
        }
      }

    });
  },
  //删除已完成||已取消的订单
  delOrderList: function (e) {
    var ths = this;
    wx2my.showModal({
      title: '',
      content: '确定要删除此订单吗？',
      confirmColor: "#eb2444",

      success(res) {
        if (res.confirm) {
          var ordernum = e.currentTarget.dataset.ordernum;
          wx2my.showLoading();
          var params = {
            url: "/p/myOrder/" + ordernum,
            method: "DELETE",
            data: {},
            callBack: function (res) {
              ths.loadOrderData(ths.data.sts, 1);
              wx2my.hideLoading();
            }
          };
          http.request(params);
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }

    });
  }
});